package br.com.sgmf.web.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import br.com.sgmf.model.Marca;
import br.com.sgmf.service.MarcaService;

@Component("marcaConverter")
public class MarcaConverter implements Converter {

	@Autowired
	private MarcaService marcaService;

	@Override
	public Marca getAsObject(FacesContext context, UIComponent component,
			String value) {
		if (StringUtils.isNotBlank(value)) {
			return marcaService.findById(Integer.valueOf(value));
		}

		return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		if (value != null && value instanceof Marca) {
			Marca marca = (Marca) value;
			return String.valueOf(marca.getId());
		}

		return null;
	}

}
